package com.ysd.service.impl;

import java.sql.Timestamp;
import java.util.Date;

import javax.mail.MessagingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ysd.entity.Verification;
import com.ysd.mapper.VerificationMapper;
import com.ysd.service.VerificationService;
import com.ysd.util.SendEmail;

@Service
public class VerificationServiceImpl implements VerificationService {

	@Autowired
	private VerificationMapper verificationMapper;

	@Override
	public Verification findUserPasswordCode(String email) {
		return verificationMapper.getUserPasswordCode(email);
	}

	@Override
	public int saveResetPasswordCode(Verification verification) {

		Integer mathNum = (int) (Math.random() * 1000000);
		if (mathNum.toString().length() < 6) {
			mathNum = mathNum * 10;
		}
		try {
			boolean isSuccess = SendEmail.sendEmailCode(verification.getV_email(), "广内殿商城找回密码验证码",
					"广内殿商城用户找回密码,你的找回验证码为[" + mathNum + "],请不要把验证码泄露给其他人,如非本人请勿操作");
			if (isSuccess) {
				Date thisTime = new Date();
				verification.setV_verificationCode(mathNum.toString());
				verification.setV_dateTime(new Timestamp(thisTime.getTime()).toString());
				return verificationMapper.insertResetPasswordCode(verification);
			} else {
				return -1;
			}
		} catch (MessagingException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Verification findEmail(Verification verification) {
		return verificationMapper.selectEmail(verification);
	}

	@Override
	public int findSta(Verification verification) {
		return verificationMapper.updateSta(verification);
	}

	
}
